package org.jeecg.modules.rental.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jeecg.modules.rental.entity.RentalCouponCategory;
import org.jeecg.modules.rental.entity.RentalCouponConfig;
import org.jeecg.modules.rental.mapper.RentalCouponCategoryMapper;
import org.jeecg.modules.rental.mapper.RentalCouponConfigMapper;
import org.jeecg.modules.rental.service.IRentalCouponConfigService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description: 卡券活动
 * @Author: jeecg-boot
 * @Date:   2024-08-19
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class RentalCouponConfigServiceImpl extends ServiceImpl<RentalCouponConfigMapper, RentalCouponConfig> implements IRentalCouponConfigService {

//    private final CacheManager cacheManager;

    private final RentalCouponCategoryMapper rentalCouponCategoryMapper;

    @Override
    public List<RentalCouponConfig> configList(RentalCouponConfig rentalCouponActivity) {
        List<RentalCouponConfig> couponConfigs = baseMapper.selectList(new QueryWrapper<RentalCouponConfig>());
        if(ObjectUtil.isNotEmpty(couponConfigs)){
//            Cache DeptCache = cacheManager.getCache(CacheConstant.SYS_DEPARTS_CACHE);
            List<String> categoryCode = couponConfigs.stream().map(RentalCouponConfig::getCategoryCode).collect(Collectors.toList());
            List<RentalCouponCategory> couponCategories = rentalCouponCategoryMapper.selectList(new QueryWrapper<RentalCouponCategory>().in("code", categoryCode));
            couponConfigs.forEach(item->{
//                if(ObjectUtil.isNotEmpty(DeptCache)){
                    //TODO 优化部门缓存
//                    SysDepartVO sysDepartVO = DeptCache.get(item.getSysOrgCode(), SysDepartVO.class);
//                    if(ObjectUtil.isNotEmpty(sysDepartVO)){
//                        item.setSysOrgName(sysDepartVO.getDepartName());
//                    }
//                }
                RentalCouponCategory couponCategory = couponCategories.stream().filter(a-> a.getCode().equals(item.getCategoryCode())).findFirst().orElse(null);
                if(ObjectUtil.isNotEmpty(couponCategory)){
                    item.setCategoryName(couponCategory.getName());
                }
            });
        }
        return couponConfigs;
    }

    @Override
    public IPage<RentalCouponConfig> selectJoinPage(Page page, Wrapper wrapper) {
        IPage<RentalCouponConfig> pageList = baseMapper.selectJoinPage(page, wrapper);
        if(ObjectUtil.isNotEmpty(pageList) && ObjectUtil.isNotEmpty(pageList.getRecords())){
            List<String> categoryCode = pageList.getRecords().stream()
                    .map(RentalCouponConfig::getCategoryCode).collect(Collectors.toList());
            List<RentalCouponCategory> couponCategories = rentalCouponCategoryMapper
                    .selectList(new QueryWrapper<RentalCouponCategory>().in(!categoryCode.isEmpty(),"code", categoryCode));
            pageList.getRecords().stream().forEach(item->{
                Optional<RentalCouponCategory>  categoryOptional = couponCategories.stream()
                        .filter(a-> a.getCode().equals(item.getCategoryCode())).findFirst();
                categoryOptional.ifPresent(a-> item.setCategoryName(a.getName()));
            });
        }
        return pageList;
    }
}
